<?php
/**
 * slide
 * @version 1.0
 * @package application
 * @subpackage application/controllers/slide/
 */
class Slide extends MY_Controller
{	
	public function __construct(){
		parent::__construct();
		$this->_rule_config=array(
			array('field'=> 'img_title' , 'label'=> '图片标题' , 'rules'=> 'required'),
			array('field'=> 'url' , 'label'=> '链接地址' , 'rules'=> 'required'),
			);

		$this->load->model('slide_model');
	}
	/**
	 * index 显示列表页
	 * @author 
	 */	
	public function index(){
		$attributes=$this->input->get();
		$search['attributes']=$attributes;
		// 总数与分页
		$total = $this->slide_model->total($search);
		$data['total'] = $total;
		// 请使用config_item( 'per_page' )获取全局显示条数
		$per_page = config_item( 'per_page' );
		$this->load->library('pagination');
		$pagination_config = array(
			'base_url' => base_url($this->_site_path.'/slide/index' ),
			'total_rows' => $total,
			'per_page' => $per_page,
			'uri_segment' => 4,
		);
		$this->pagination->initialize( $pagination_config );
		$data['pagination'] = $this->pagination->create_links();

		$search['limit']=array('persize'=>$per_page,'offset'=>$this->pagination->get_cur_offset());
		$data['slide_data']=$this->slide_model->all($search);
		$this->load->view($this->_site_path.'/slide/index',$data);
	}

	/**
	 * view 显示详细页
	 * @param $id 主键值
	 * @author 
	 */
	public function view($id)
	{
		$data['slide_data']=$this->slide_model->get_by_pk($id);
		$this->load->view($this->_site_path.'/slide/view',$data);
	}

	/**
	 * add 添加 
	 * @author 
	 */
	public function add()
	{
		$data = array( );

		if ( $this->input->post( 'is_submit' ) && $this->form_validation->check_token() ){
			try {

				$slide_data = $this->input->post();
				//设置验证规则
				$this->form_validation->set_rules($this->_rule_config);
				if ( !$this->form_validation->run() ) throw new Exception( validation_errors(), 0 );
				$result=$this->upload_file('img_url');
				if(!$result['flag']) throw new Exception($result['msg'], 0);
				$slide_data['img_url']=$result['msg'];
				$slide_data['created']=$slide_data['modified']=time();
				$data['slide_data'] = $slide_data;	
				//插入
				$slide_id = $this->slide_model->insert( $slide_data );
				if($slide_data['sort'] == "")
				{	
					$this->slide_model->update_by_pk(array('sort'=>$slide_id),$slide_id);
					// $old_data=$this->slide_model->get_value_by_sortid($slide_data['sort']);
					// if($old_data)
					// {
					// 	$this->slide_model->update_by_pk(array('sort'=>$slide_data['sort']),$old_data['id']);
					// }	
				}
				init_messagebox( '添加成功', 'success', 1, base_url( $this->_site_path.'/slide/index' ) );
			} catch ( Exception $e ) {
				init_messagebox( $e->getMessage(), 'error', $e->getCode() );
			}
		}
		$this->load->view($this->_site_path.'/slide/form', $data );
	}

	/**
	 * edit 编辑 
	 * @param $id 主键值
	 * @author 
	 */
	public function edit($id)
	{
		$data = array( );
		$data['slide_data']=$this->slide_model->get_by_pk($id);
		if ( $this->input->post( 'is_submit' ) && $this->form_validation->check_token() ){
			try {
				$slide_data = $this->input->post();
				//设置验证规则
				$this->form_validation->set_rules($this->_rule_config);
				if ( !$this->form_validation->run() ) throw new Exception( validation_errors(), 0 );
				if($_FILES['img_url']['name'] !='')
				{
					$result=$this->upload_file('img_url');
					if(!$result['flag']) throw new Exception($result['msg'], 0);
					$slide_data['img_url']=$result['msg'];
				}

				if($slide_data['sort'] == "" || $slide_data['sort'] == $id)
				{
					$slide_data['sort']=$id;
				}
				// else
				// {
				// 	$old_data=$this->slide_model->get_value_by_sortid($slide_data['sort']);
				// 	if($old_data)
				// 	{
				// 		$this->slide_model->update_by_pk(array('sort'=>$slide_data['sort']),$old_data['id']);
				// 	}	
				// }
				$slide_data['modified']=time();
				$data['slide_data'] = $slide_data;
				//修改
				$this->slide_model->update_by_pk( $slide_data ,$id);
				init_messagebox( '修改成功', 'success', 1, base_url( $this->_site_path.'/slide/index' ) );
			} catch ( Exception $e ) {
				init_messagebox( $e->getMessage(), 'error', $e->getCode() );
			}
		}
		$this->load->view($this->_site_path.'/slide/form', $data );
	}	

	/**
	 * delete 删除
	 * @param $id 主键值
	 * @author 
	 */
	public function delete( $id ) {
		$this->slide_model->delete_by_pk( $id );
		redirect( base_url($this->_site_path.'/slide/index' ) );
	}

	/**
	 * delete_all 批量删除
	 * @param $id 主键值
	 * @author 
	 */
	public function delete_all() {
		$ids=$this->input->post('ids');
		foreach($ids as $id){
			$this->slide_model->delete_by_pk( $id );
		}
		redirect( base_url($this->_site_path.'/slide/index' ) );
	}


	   /**
	 * 文件上传函数
	 * @param  [string] $field [文件域的名称]
	 * @return [array]        [上传文件后返回的结果数组]
	 */
	public function upload_file($field)
	{		
		  $config['upload_path'] ='./uploads/';
		  $config['allowed_types'] = 'gif|jpg|png';
		  $config['max_size'] = '1000';
		  $config['max_width']  = '1000';
		  $config['max_height']  = '400';
		  $config['file_name']=date('YmdHis',time());
		  $this->load->library('upload', $config);
		  if (!$this->upload->do_upload($field))
  		  {
  			 $result = array('flag'=>0,'msg' => $this->upload->display_errors());
    	  }
    	  else
    	  {
    	  	$data=$this->upload->data();
    	  	$result=array('flag'=>1,'msg'=>substr($config['upload_path'], 1) . $data['file_name']);
    	  }
    	  return $result;
    }
}
